<?php

namespace easydis\APSBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use easydis\APSBundle\Entity\Salarie;
use easydis\APSBundle\Entity\Contrat;
use easydis\APSBundle\Entity\TypeContrat;
use easydis\APSBundle\Form\SalarieType;
use easydis\APSBundle\Form\UploadType;
use Symfony\Component\HttpFoundation\Session\Session;

class RHController extends Controller
{
    public function indexAction() {
        return $this->render('easydisAPSBundle:RH:choix_vue.html.twig');
    }
    
    public function listeAction($choix) {

        $em = $this->getDoctrine()->getManager();

        $repository_salarie = $em->getRepository('easydisAPSBundle:Salarie');

        $tabResult = array(array());
        
        $tabResult[0][0] = "";
        $tabResult[0][1] = "";
        $tabResult[0][2] = "";
        $tabResult[0][3] = "";
        $tabResult[0][4] = "";
            
        $ligne = 0;
        $list_salarie = $repository_salarie->get_list_effectif('1', 'site');
        foreach($list_salarie as $salarie) {
            //Recuperation de la ligne Sous-total niveau groupe de service
            $tabResult[$ligne][0] = $salarie->getMatricule();
            $tabResult[$ligne][1] = $salarie->getNom();
            $tabResult[$ligne][2] = $salarie->getPrenom() . ' ' . $salarie->getPrenom2() . ' ' . $salarie->getPrenom3();
            $tabResult[$ligne][3] = $salarie->getSection()->getLib();
            $tabResult[$ligne][4] = $salarie->getContrat()->getTypeContrat()->getLib();
            $ligne = $ligne + 1;
        }
        return $this->render('easydisAPSBundle:RH:liste_effectif.html.twig', array('tabResult' => $tabResult));
    }
    
    public function ajouter_salarieAction($idExploit) {
        $em = $this->getDoctrine()->getManager();
        
        $salarie = new Salarie;
        $form = $this->createForm(new SalarieType($idExploit), $salarie);
        
        $request = $this->get('request');
        if ($request->getMethod() == 'POST') {
          $form->bind($request);

          if ($form->isValid()) {
            $em->persist($salarie);
            $em->flush();

            return $this->redirect($this->generateUrl('easydisAPSBundle_routing_ajout_succes'));
          }
        }
        
        return $this->render('easydisAPSBundle:RH:ajouter_salarie.html.twig', array('form' => $form->createView(), 'idExploit' => $idExploit));
    }
    
    public function importation_salaries_rhAction() {
        $em = $this->getDoctrine()->getManager();
        $user = $this->get('security.context')->getToken()->getUser();
        
        $repository_site = $em->getRepository('easydisAPSBundle:Site');
        $repository_section = $em->getRepository('easydisAPSBundle:Section');
        $repository_contrat = $em->getRepository('easydisAPSBundle:Contrat');
        $repository_typecontrat = $em->getRepository('easydisAPSBundle:TypeContrat');
        $repository_salarie = $em->getRepository('easydisAPSBundle:Salarie');
        
        $form = $this->createForm(new UploadType());
        
        $request = $this->get('request');
        if ($request->getMethod() == 'POST') {
            $form->bind($request);

            if ($form->isValid()) {
                $file = $form['attachment']->getData();
                $row = 0;
                $ligne = 0;
                $matricule = "";
                $type_contrat = new TypeContrat();
                $tab = array();
                if (($handle = fopen($file, "r")) !== FALSE) {
                    while (($data = fgetcsv($handle, 3000, ";")) !== FALSE) {
                        $num = count($data);
                        if(7 <= $row) {
                            $matriculetmp = substr($data[4], 0, strlen($data[4])-1);
                            if ($matricule != $matriculetmp) {
                                $lib_section = substr($data[1], -4, 4);
                                $type_contrat_lib = $data[count($data)-1];
                                $keywords = preg_split("/,/", $data[5]);
                                $matricule = $matriculetmp;
                                $site = $repository_site->findBy(array('id' => $user->getExploit()->getSite()->getId()));
                                $liste_section = $repository_section->get_section($site[0]->getId(), 'site');     
                                $section = null;
                                foreach($liste_section as $element) {
                                    if($element->getLib() == $lib_section) {
                                        $section = $element;
                                        break;
                                    }
                                }
                                if($section == null) {
                                    $tab[$ligne] = 'Ligne '.$row.' : Erreur. La section '.$lib_section.' est introuvable.';
                                } else {
                                    if($type_contrat->getLib() != $type_contrat_lib) {
                                        $type_contrat = $repository_typecontrat->findOneBy(array('lib' => $type_contrat_lib));
                                    }
                                    if($type_contrat == null) {
                                        $tab[$ligne] = 'Ligne '.$row.' : Erreur. Le type de contrat '.$type_contrat_lib.' est introuvable.';
                                        $type_contrat = new TypeContrat();
                                    } else {
                                        $contrat = $repository_contrat->findOneBy(array('typecontrat'=>$type_contrat));
                                        if($contrat == null) {
                                            $contrat = new Contrat();
                                            $contrat->setDateDebut('11/02/2014');
                                            $contrat->setTypeContrat($type_contrat);   
                                        }

                                        $salarie = $repository_salarie->findOneBy(array('matricule' => $matricule));
                                        if($salarie == null) {
                                           $salarie = new Salarie();
                                        }
                                        $salarie->set_all($matricule, $keywords[0], 'NONE', $section, $contrat);
                                        if(isset($keywords[1]))
                                            $salarie->setPrenom($keywords[1]);
                                        if(isset($keywords[2]))
                                            $salarie->setPrenom2($keywords[2]);
                                        if(isset($keywords[3]))
                                            $salarie->setPrenom3($keywords[3]);
                                        $em->persist($salarie);
                                        $em->flush();
                                        $tab[$ligne] = 'Ligne '.$row.'. '.$salarie->getMatricule().' | '.$salarie->getNom().','.$salarie->getPrenom().' | '.$salarie->getSection()->getLib();
                                    }
                                }
                                $ligne++;
                            }
                        }
                        $row++;
                    }
                    fclose($handle);
                    return $this->render('easydisAPSBundle:Routing:ajout_succes.html.twig', array('tab' => $tab));
                }
            }
        }
        return $this->render('easydisAPSBundle:RH:importer_salarie.html.twig', array('form' => $form->createView()));
    }
}